package com.java110.mall.cart.cmd.storeOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.product.ProductSpecDetailDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.goods.IProductSpecDetailInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询订单信息
 */
@Java110Cmd(serviceCode = "storeOrder.queryStoreOrderCart")
public class QueryStoreOrderCartCmd extends Cmd {

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private IProductSpecDetailInnerServiceSMO productSpecDetailInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含门店编号");

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        // 将link 转为personId
        String link = reqJson.getString("link");
        if (!StringUtil.isEmpty(link)) {
            UserDto userDto = new UserDto();
            userDto.setTel(link);
            List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
            if (ListUtil.isNull(userDtos)) {
                return;
            }
            reqJson.put("personId", userDtos.get(0).getUserId());
            reqJson.remove("link");
        }

        String states = reqJson.getString("states");
        if (reqJson.containsKey("states")) {
            reqJson.remove("states");
        }
        StoreOrderCartDto storeOrderCartDto = BeanConvertUtil.covertBean(reqJson, StoreOrderCartDto.class);

        if (!StringUtil.isEmpty(states)) {
            storeOrderCartDto.setStates(states.split(","));
        }

        int count = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCartsCount(storeOrderCartDto);

        List<StoreOrderCartDto> storeOrderCartDtos = null;
        if (count > 0) {
            storeOrderCartDtos = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCarts(storeOrderCartDto);
            freshOrderCart(storeOrderCartDtos);
        } else {
            storeOrderCartDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeOrderCartDto.getRow()), count, storeOrderCartDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    /**
     * 刷新规格
     *
     * @param storeOrderCartDtos
     */
    private void freshOrderCart(List<StoreOrderCartDto> storeOrderCartDtos) {

        List<String> specIds = new ArrayList<>();

        for (StoreOrderCartDto storeOrderCartDto : storeOrderCartDtos) {
            specIds.add(storeOrderCartDto.getSpecId());
        }

        ProductSpecDetailDto productSpecDetailDto = new ProductSpecDetailDto();
        productSpecDetailDto.setSpecIds(specIds.toArray(new String[specIds.size()]));
        List<ProductSpecDetailDto> productSpecDetailDtos = productSpecDetailInnerServiceSMOImpl.queryProductSpecDetails(productSpecDetailDto);
        List<ProductSpecDetailDto> tmpProductSpecDetailDtos = null;
        if (!ListUtil.isNull(productSpecDetailDtos)) {
            for (StoreOrderCartDto storeOrderCartDto : storeOrderCartDtos) {
                if (StoreOrderCartDto.CART_TYPE_SERV.equals(storeOrderCartDto.getCartType())) {
                    continue;
                }
                tmpProductSpecDetailDtos = new ArrayList<>();
                for (ProductSpecDetailDto tmpProductSpecDetailDto : productSpecDetailDtos) {
                    if (!StringUtil.isEmpty(storeOrderCartDto.getSpecId()) && storeOrderCartDto.getSpecId().equals(tmpProductSpecDetailDto.getSpecId())) {
                        tmpProductSpecDetailDtos.add(tmpProductSpecDetailDto);
                    }
                }
                storeOrderCartDto.setProductSpecDetails(tmpProductSpecDetailDtos);
            }
        }

        //输入
        List<String> productIds = new ArrayList<>();

        for (StoreOrderCartDto tmpProductDto : storeOrderCartDtos) {
            productIds.add(tmpProductDto.getProductId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (StoreOrderCartDto tmpProductDto : storeOrderCartDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (!tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    continue;
                }

                if (StringUtil.isEmpty(tmpFileRelDto.getFileRealName())) {
                    continue;
                }
                url = imgUrl + tmpFileRelDto.getFileRealName();
                if (tmpFileRelDto.getFileRealName().startsWith("http")) {
                    url = tmpFileRelDto.getFileRealName();
                }

                tmpProductDto.setCoverPhoto(url);
            }
        }
    }
}
